package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.PayService;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Objects;

/**
 * @author zy
 */
@Service
public class PayServiceImpl implements PayService {


    @Autowired
    SysUserMapper sysUserMapper;

    @Override
    public boolean pay(Map<String, String> dataMap) {
        SysUser sysUser = sysUserMapper.selectUserByCardNumber(dataMap.get("cardNumber"));
        if (sysUser == null) {
            throw new BusinessException("会员卡号不存在");
        } else {
            String type = dataMap.get("type");
            DateTime dateTime = new DateTime(sysUser.getCardEndDate());
            if (Objects.equals(type, PayCode.MONTH.getCode())) {
                dateTime = dateTime.plusMonths(PayCode.MONTH.getTime());
                sysUser.setCardEndDate(dateTime.toDate());
            } else if (Objects.equals(type, PayCode.RD_3_MONTH.getCode())) {
                dateTime = dateTime.plusMonths(PayCode.RD_3_MONTH.getTime());
                sysUser.setCardEndDate(dateTime.toDate());
            } else if (Objects.equals(type, PayCode.YEAR.getCode())) {
                dateTime = dateTime.plusMonths(PayCode.MONTH.getTime());
                sysUser.setCardEndDate(dateTime.toDate());
            }
            sysUserMapper.updateUserById(sysUser);
            return true;
        }
    }

}

/**
 * 支付枚举
 */
enum PayCode {
    /**
     * 按月计算
     * 月 季度 年
     */
    MONTH("1", 1),
    RD_3_MONTH("2", 3),
    YEAR("3", 12);

    PayCode(String code, Integer time) {
        this.code = code;
        this.time = time;
    }

    private final String code;
    private final Integer time;

    public String getCode() {
        return code;
    }

    public Integer getTime() {
        return time;
    }
}